import request from "@/utils/request"
import {AxiosPromise} from "axios";
import {QuizForm, QuizInfo, QuizRecommend, QuizSearch} from "./types";
import {PageQuery, Result, TableDataInfo} from "@/model";

/**
 * 查看题目列表
 * @param params 查询条件
 * @returns 题目列表
 */
export function getQuizList(params: PageQuery): AxiosPromise<TableDataInfo> {
    return request({
        url: "/quiz/list",
        method: "get",
        params,
    });
}

/**
 * 查看记录
 * @param quizId 记录id
 * @returns 记录列表
 */
export function getQuizHomeById(quizId: number): AxiosPromise<Result<QuizInfo>> {
    return request({
        url: `/quiz/${quizId}`,
        method: "get",
    });
}

/**
 * 查看记录总览
 * @returns 记录总览列表
 */
export function getQuizOverviewList(): AxiosPromise<TableDataInfo> {
    return request({
        url: "/quiz/overview/list",
        method: "get",
    });
}

/**
 * 查看推荐记录
 * @returns 推荐记录
 */
export function getQuizRecommend(): AxiosPromise<Result<QuizRecommend[]>> {
    return request({
        url: "/quiz/recommend",
        method: "get",
    });
}

/**
 * 搜索记录
 * @returns 记录列表
 */
export function searchQuiz(keyword: string): AxiosPromise<Result<QuizSearch[]>> {
    return request({
        url: "/quiz/search",
        method: "get",
        params: {
            keyword,
        },
    });
}

/**
 * 点赞记录
 * @param quizId 记录id
 */
export function likeQuiz(quizId: number): AxiosPromise<Result<null>> {
    return request({
        url: `/quiz/${quizId}/like`,
        method: "post",
    });
}

/**
 * 收藏记录
 * @param quizId 记录id
 */
export function collectQuiz(quizId: number): AxiosPromise<Result<null>> {
    return request({
        url: `/quiz/${quizId}/collect`,
        method: "post",
    });
}

/**
 * 添加题目
 * @param data 题目信息
 */
export function addQuiz(data: QuizForm): AxiosPromise<Result<null>> {
    return request({
        url: "/quiz/add",
        method: "post",
        data
    });
}

/**
 * 删除题目
 * @param data 题目id
 */
export function deleteQuiz(data: number[]): AxiosPromise<Result<null>> {
    return request({
        url: "/quiz/delete",
        method: "delete",
        data,
    });
}

/**
 * 修改题目
 * @param data 题目信息
 */
export function updateQuiz(data: QuizForm): AxiosPromise<Result<null>> {
    return request({
        url: "/quiz/update",
        method: "put",
        data,
    });
}

/**
 * 编辑题目
 * @param quizId 题目id
 */
export function editQuiz(quizId: number): AxiosPromise<Result<QuizForm>> {
    return request({
        url: `/quiz/edit/${quizId}`,
        method: "get",
    });
}

/**
 * 上传记录图片
 * @returns 图片链接
 */
export function uploadQuizImages(data: FormData): AxiosPromise<Result<string>> {
    return request({
        url: "/quizImage/upload",
        headers: {"content-type": "multipart/form-data"},
        method: "post",
        data,
    });
}